﻿using BaseUI;
using BLL;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.Templete
{
    public partial class Sys_RoleGroupManage : BasePage
    {
        private Sys_RoleBLL roleBLL = new Sys_RoleBLL();
        private Sys_RoleGroupBLL srgb = new Sys_RoleGroupBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["SortOrder"] = "Rgro_Id";
                ViewState["OrderDire"] = "asc";
                //BindGridView();
                BindData(false);
            }
        }

        /// <summary>
        /// 绑定GridView数据源
        /// </summary>
        //public void BindGridView()
        //{
        //    txtRoleGroupName.Text = "";
        //    lblRID.Value = "";
        //    //txtRoleName.Text = "";
        //    ddlRoleState.SelectedValue = "-1";
        //    DataTable dt = srgb.GetAllInfoRole();

        //    gvRole.DataSource = dt;
        //    gvRole.DataBind();
        //}
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                string strCon = "";
                strCon += txtRoleGroupName.Text + ",";
                strCon += ddlRoleState.SelectedItem.Text;

                ViewState["Where"] = strCon;
                BindData(true);
                //DataTable dt = srgb.GetInfoByCondition3("Condition", strCon);
                //gvRole.DataSource = dt;
                //gvRole.DataBind();
            }
            catch (Exception)
            {
                //lblMes.Text = "连接数据库失败，请联系管理员！";
                MessageBox.Show(Page, "连接数据库失败，请联系管理员！！！");
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            MessageBox.ResponseScript(this, "FORM_INSERT();");
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int count = 0;
            for (int i = 0; i < this.gvRole.Rows.Count; i++)
            {
                if (((CheckBox)this.gvRole.Rows[i].FindControl("chkSelectDel")).Checked == true)
                {
                    count++;
                    if (count == 1)
                    {
                        string pid = ((HiddenField)gvRole.Rows[i].FindControl("HiddID")).Value.ToString();
                        MessageBox.ResponseScript(this, "FORM_EDIT(\"" + pid + "\");");
                        return;
                    }
                }
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelele_Click(object sender, EventArgs e)
        {
            try
            {
                int count = 0;
                int row = 0;
                for (int i = 0; i < this.gvRole.Rows.Count; i++)
                {
                    if (((CheckBox)this.gvRole.Rows[i].FindControl("chkSelectDel")).Checked == true)
                    {
                        count++;
                        if (count == 1)
                        {
                            string pid = ((HiddenField)gvRole.Rows[i].FindControl("HiddID")).Value.ToString();
                            row = srgb.DELETE(pid, base.lognUser.USER_NAME);
                        }
                    }
                }
                if (row > 0)
                {
                    MessageBox.Show(Page, "删除成功！");
                    BindData(true);
                }
                else
                {
                    MessageBox.Show(Page, "删除失败！");
                    //lblMes.Text = "删除失败！";
                }
            }
            catch (Exception)
            {
                MessageBox.Show(Page, "连接数据库失败，请联系管理员！");
            }
        }

        /// <summary>
        /// gvRole_PageIndexChanging
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvRole_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                gvRole.PageIndex = e.NewPageIndex;
                //BindGridView();
                BindData(true);
                TextBox tb = (TextBox)gvRole.BottomPagerRow.FindControl("inPageNum");
                tb.Text = (gvRole.PageIndex + 1).ToString();
            }
            catch { }
        }

        /// <summary>
        /// gvRole_RowCommand
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvRole_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "go")
            {
                try
                {
                    TextBox tb = (TextBox)this.gvRole.BottomPagerRow.FindControl("inPageNum");
                    int num = Int32.Parse(tb.Text);
                    GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
                    gvRole_PageIndexChanging(null, ea);
                }
                catch { }
            }
        }

        /// <summary>
        /// GridView 标题排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvRole_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["SortOrder"] == null ? null : (string)ViewState["SortOrder"];
            OrderDire = ViewState["OrderDire"] == null ? null : (string)ViewState["OrderDire"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "Desc")
                    OrderDire = "ASC";
                else
                    OrderDire = "Desc";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["SortOrder"] = SortOrder;
            ViewState["OrderDire"] = OrderDire;
            //BindProjectName(true);
            BindData(true);
        }

        /// <summary>
        /// 绑定项目名称（非动态）绑定GridView
        /// </summary>
        //public void BindProjectName(bool iSsort)
        //{
        //    string strCon = "";
        //    strCon += txtRoleGroupName.Text + ",";
        //    strCon += ddlRoleState.SelectedItem.Text;//.SelectedValue;

        //    DataTable dt = srgb.GetInfoByCondition3("Condition", strCon);

        //    DataView view = dt.DefaultView;
        //    if (iSsort)
        //    {
        //        string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
        //        view.Sort = sort;//设置排序
        //    }

        //    gvRole.DataSource = view;

        //    gvRole.DataBind();
        //}

        //分页
        protected void PageIng_DataBinding(object sender, EventArgs e)
        {
            //BindProjectName(false);
            BindData(true);
        }

        private void BindData(bool isFirst)
        {
            var Type = "";
            string strCon = "";
            try
            {
                if (!string.IsNullOrEmpty(ViewState["Where"].ToString()))
                {
                    Type = "Condition";
                    strCon = ViewState["Where"].ToString();
                }
                else
                {
                    Type = "RoleIds";
                }
            }
            catch (Exception)
            {
                Type = "";
            }

            int rowcount = 0;
            if (isFirst == false)
            {
                PageIng.CurrentPage = 1;
            }
            rowcount = srgb.GetRecordCount(Type, strCon);

            string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];

            DataTable dt = srgb.GetListByPage(Type, strCon, sort, PageIng.CurrentPage, PageIng.RowsPerPage);
            ViewState["database"] = dt;
            if (dt.Rows.Count > 0)
            {
                PageIng.Visible = true;
                PageIng.RowsCount = rowcount;
                PageIng.DataSource = dt;
                PageIng.GridViewSource = gvRole;
                PageIng.RowsPerPage = PageIng._rowsPerPage;
                PageIng.Bind();
            }
            else
            {
                PageIng.DataSource = dt;
                PageIng.GridViewSource = gvRole;
                gvRole.DataSource = null;
                gvRole.DataBind();
                PageIng.Visible = false;
            }
        }
    }
}